package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.article.Article;
import com.zbkj.common.model.traceability.TraceabilityLogs;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.OrderTraceLogsRecordFrontSearchRequest;
import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.response.OrderTraceLogsRecordFrontPageDataResponse;
import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.model.agent.OrderTraceLogsRecord;
import com.zbkj.service.dao.OrderTraceLogsRecordDao;
import com.zbkj.service.service.OrderTraceLogsRecordService;

import com.zbkj.common.request.OrderTraceLogsRecordSaveRequest;
import com.zbkj.common.request.OrderTraceLogsRecordSearchRequest;
import com.zbkj.common.response.OrderTraceLogsRecordPageDataResponse;
import com.zbkj.common.response.OrderTraceLogsRecordDetailResponse;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @author Caiyc
* @description OrderTraceLogsRecordServiceImpl 接口实现
* @date 2025-08-04
*/
@Service
public class OrderTraceLogsRecordServiceImpl extends ServiceImpl<OrderTraceLogsRecordDao, OrderTraceLogsRecord> implements OrderTraceLogsRecordService {

    @Resource
    private OrderTraceLogsRecordDao dao;

    @Override
    public Boolean create(OrderTraceLogsRecordSaveRequest orderTraceLogsRecordSaveRequest) {
        OrderTraceLogsRecord orderTraceLogsRecord = new OrderTraceLogsRecord();
        BeanUtils.copyProperties(orderTraceLogsRecordSaveRequest, orderTraceLogsRecord, "id");
        boolean save = save(orderTraceLogsRecord);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询订单溯源号段表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<OrderTraceLogsRecord>
     * @author Caiyc
     * @since 2025-08-04
     */
    @Override
    public PageInfo<OrderTraceLogsRecordPageDataResponse> getList(OrderTraceLogsRecordSearchRequest request, PageParamRequest pageParamRequest) {
        Page<Article> articlePage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<OrderTraceLogsRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(OrderTraceLogsRecord::getIsDel, false);

        lambdaQueryWrapper.orderByDesc(OrderTraceLogsRecord::getCreateTime);
        List<OrderTraceLogsRecord> orderTraceLogsRecords = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<OrderTraceLogsRecordPageDataResponse> orderTraceLogsRecordResponses = new ArrayList<>();

        orderTraceLogsRecords.stream().forEach(x->{
            OrderTraceLogsRecordPageDataResponse orderTraceLogsRecordResponse = new OrderTraceLogsRecordPageDataResponse();
            BeanUtils.copyProperties(x, orderTraceLogsRecordResponse);
            orderTraceLogsRecordResponses.add(orderTraceLogsRecordResponse);
        });
        return CommonPage.copyPageInfo(articlePage, orderTraceLogsRecordResponses);
    }

    /**
     * 分页查询订单溯源号段表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<OrderTraceLogsRecord>
     * @author Caiyc
     * @since 2025-08-04
     */
    @Override
    public PageInfo<OrderTraceLogsRecordFrontPageDataResponse> getListH5(OrderTraceLogsRecordFrontSearchRequest request, PageParamRequest pageParamRequest) {
        Page<TraceabilityLogs> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<OrderTraceLogsRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(OrderTraceLogsRecord::getIsDel, false);
        lambdaQueryWrapper.eq(OrderTraceLogsRecord::getOrderNo, request.getOrderNo());
        lambdaQueryWrapper.orderByDesc(OrderTraceLogsRecord::getCreateTime);
        List<OrderTraceLogsRecord> orderTraceLogsRecords = dao.selectList(lambdaQueryWrapper);
        List<OrderTraceLogsRecordFrontPageDataResponse> orderTraceLogsRecordResponses = new ArrayList<>();

        orderTraceLogsRecords.stream().forEach(x->{
            OrderTraceLogsRecordFrontPageDataResponse orderTraceLogsRecordResponse = new OrderTraceLogsRecordFrontPageDataResponse();
            BeanUtils.copyProperties(x, orderTraceLogsRecordResponse);
            orderTraceLogsRecordResponses.add(orderTraceLogsRecordResponse);
        });
        return CommonPage.copyPageInfo(page, orderTraceLogsRecordResponses);
    }

    @Override
    public PageInfo<OrderTraceLogsRecordFrontPageDataResponse> getListByOrderNo(String orderNo, PageParamRequest pageParamRequest) {
        Page<TraceabilityLogs> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<OrderTraceLogsRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(OrderTraceLogsRecord::getOrderNo, orderNo);
        lambdaQueryWrapper.eq(OrderTraceLogsRecord::getIsDel, false);
        lambdaQueryWrapper.orderByDesc(OrderTraceLogsRecord::getCreateTime);
        List<OrderTraceLogsRecord> orderTraceLogsRecords = dao.selectList(lambdaQueryWrapper);
        List<OrderTraceLogsRecordFrontPageDataResponse> orderTraceLogsRecordResponses = new ArrayList<>();

        orderTraceLogsRecords.stream().forEach(x->{
            OrderTraceLogsRecordFrontPageDataResponse orderTraceLogsRecordResponse = new OrderTraceLogsRecordFrontPageDataResponse();
            BeanUtils.copyProperties(x, orderTraceLogsRecordResponse);
            orderTraceLogsRecordResponses.add(orderTraceLogsRecordResponse);
        });
        return CommonPage.copyPageInfo(page, orderTraceLogsRecordResponses);
    }

    /**
    * 修改订单溯源号段表
    * @param request
    * @return
    */
    @Override
    public boolean update(OrderTraceLogsRecordSaveRequest request) {
        if (ObjectUtil.isNull(request.getId()) || request.getId() < 1) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "请传入订单溯源号段表ID");
        }
        getByIdException(request.getId());
        OrderTraceLogsRecord orderTraceLogsRecord = new OrderTraceLogsRecord();
        BeanUtils.copyProperties(request, orderTraceLogsRecord);
        return updateById(orderTraceLogsRecord);
    }

    @Override
    public OrderTraceLogsRecord getByIdException(Integer id) {
        OrderTraceLogsRecord orderTraceLogsRecord = getById(id);
        if (ObjectUtil.isNull(orderTraceLogsRecord)) {
            throw new AppException("订单溯源号段不存在");
        }
        if (orderTraceLogsRecord.getIsDel()) {
            throw new AppException("订单溯源号段不存在");
        }
        return orderTraceLogsRecord;
    }

    /**
    * 批量删除
    * @param idList
    * @return
    */
    @Override
    public boolean batchDelete(List<Integer> idList) {
        LambdaUpdateWrapper<OrderTraceLogsRecord> luw = Wrappers.lambdaUpdate();
        luw.set(OrderTraceLogsRecord::getIsDel, true);
        luw.in(OrderTraceLogsRecord::getId, idList);
        boolean update = update(luw);
        if (!update) {
            throw new AppException(CommonResultCode.ERROR.setMessage("批量删除订单溯源号段表失败"));
        }
        return true;
    }

    /**
    * 查询详情
    * @param id
    * @return
    */
    @Override
    public OrderTraceLogsRecordDetailResponse getDetails(Integer id) {
        LambdaQueryWrapper<OrderTraceLogsRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(OrderTraceLogsRecord::getIsDel, 0);
        lqw.eq(OrderTraceLogsRecord::getId, id);
        lqw.last("limit 1");
        OrderTraceLogsRecord orderTraceLogsRecord = this.getOne(lqw);
        if (ObjectUtil.isNotNull(orderTraceLogsRecord)) {
            OrderTraceLogsRecordDetailResponse response = new OrderTraceLogsRecordDetailResponse();
            BeanUtils.copyProperties(orderTraceLogsRecord, response);
            return response;
        }
        return null;
    }


}

